
const author = require("./author.js")
const SignHeader = require("./signHeader.js")
const config = require("../config/config.js")

// 隐藏授权弹窗
const hideAuthorModal = function () {
  let pages = getCurrentPages()
  if (pages.length > 0) {
    console.log("弹出授权弹窗")
    pages[0].onNeedAuthor && pages[0].onNeedAuthor()
    pages[0].setData({
      needAuthor: false
    })
  }
}

// 1.检查网络状态
const checkNetStatus= function(res){
  if (res.statusCode == 200) {
    return true
  } else {
    return false
  }
}

// 2.处理登录异常
//  1009 没有授权渠道
//  3005 没有找到该用户
//  1005 token 失效
//  1006 token 失效
const handleLoginError=function(res){
  if (res.code == 1000) {
    return true
  } else {
    return false
  }
}

export let fetch = (options)=>{
   let params = options.params
   let url = options.url
   let data = options.data||{}
   let method = options.method || 'get'
   let arr = []
   if(typeof params == 'object' && params != null){
     let keys = Object.keys(params)
     for(let key of keys){
       arr.push(`${key}=${params[key]}`)
     }
     let queryString = arr.join('&')
     url = url.indexOf('?')!=-1 ? url + queryString: `${url}?${queryString}`
   }
  return request(method,url,data)
}

// 请求数据
export const request = function(method, url, params = {}){
  return new Promise((resolve, reject) => {
    var token = wx.$db.token
    if((token == "" || token == null) && !params.noNeedLogin){
      wx.hideLoading()
      author.login()
      return
    }
    var data = params
    if (data == null) {
      data = {}
    }
    // data.subChannelNo = config.subChannelNo
    let header = SignHeader(params, token)
    header['x-access-token'] = token
    wx.request({
      url: config.baseUrl+url,
      header: header,
      method: method,
      data: data,
      dataType: "json",
      success: (data) => {
        console.log(data,'xxxxx')
        if (checkNetStatus(data)) {
          let res = data.data
          if (!handleLoginError(res)) {
            if (res.code == 0) {
              // 隐藏弹窗
              // hideAuthorModal()
              resolve(res)
            } else {
              let msg = res.message
              
              if(params.pass){
                reject && reject(data)
              }else{
                wx.$showToast(msg)
                reject && reject(data)
              }
            }
          }else{
            wx.$db.token = ''
            author.login()
            wx.hideLoading()
           
          }
        }else{
     
          if (data.statusCode==500){
           
          }
          wx.$showToast("~系统正在维护中,请稍后~")
        }
      },
      fail: function (err) {
        console.log("dddd",err)
        
        wx.$showToast("请检查手机网络")
      }
    })
  })
}

// 4.拼接路径
const generateMatchApi = function(api, querys) {
  // 第一步查找路径中的参数名
  var matchs = api.match(/\{[^\}]+\}/g);
  if (matchs == null) {
    return api
  }
  var params = matchs.map(res => {
    return res.replace(/\{/g, '').replace(/\}/g, '')
  })

  var result = api
  // 替换掉路径中参数名
  for (var i in matchs) {
    var key = params[i]
    if (querys == null) {
      console.log("querys为空")
      return api
    }
    var value = querys[key]
    if (value == null) {
      console.log("api:" + api + "参数:" + key + "缺失")
    }
    result = result.replace(new RegExp(matchs[i], "g"), value)
  }
  return result
}

export let get=(api,params,querys)=>{
  var path = generateMatchApi(api,querys)
  return request("GET",path,params)
}

export let post=(api, params, querys)=>{
  var path = generateMatchApi(api, querys)
  return request("POST", path, params)
}

 // 权限post
 export let postUsingAuthor = (api, params, querys) =>{
  console.log(wx.$db.userType)
  if (wx.$db.userType != 2) {
    wx.hideLoading()
    let pages = getCurrentPages()
    let sourceUrl = ""
    if (pages.length > 0) {
      console.log(pages[0])
      let query = []
      for (let k in pages[0].options) {
        query.push(`${k}=${pages[0].options[k]}`)
      }
      sourceUrl = pages[0].route + "?" + query.join("&")
    }
    wx.navigateTo({
      url: `/pages/author/author?sourceUrl=${sourceUrl}`,
    })
    return new Promise((resolve, reject) => {
    })
  } else {
    var path = generateMatchApi(api, querys)
    return request("POST", path, params)
  }
}


// PUT 请求
export let put=(api, params, querys)=>{
  var path = generateMatchApi(api, querys)
  return request("PUT", path, params)
}

// 删除操作
export let Delete = (api, params, querys)=>{
  var path = generateMatchApi(api, querys)
  return request("DELETE", path, params)
}

export let  upload=(url,filePath,params={})=>{
  var token = wx.$db.token
  let header = SignHeader({}, token)
  header['Content-type'] = 'multipart/form-data'
  if(filePath==null||filePath==''){
     wx.showToast({
       title: 'filePath is Null',
       icon:"none",
       duration:1500
     })
     return
  }
  if(url==null||url==''){
    wx.showToast({
      title: 'url is Null or empty',
      icon: "none",
      duration: 1500
    })
    return
  }

  return new Promise(function (resolve, reject){
    wx.uploadFile({
      url: url, 
      filePath: filePath,
      name: 'file',
      header: header,
      formData: params,
      success: function (data) {
        console.log(data)
        if (checkNetStatus(data)) {
          let res = JSON.parse(data.data) 
          if (!handleLoginError(res)) {
            if (res.code == 0) {
              console.log("----")
             
              resolve(res)
            } else {
              wx.$showToast(res.exception.message)
              reject(data)
            }
          }else{
            reject(data)
          }
        }else{
          reject(data)
        }
     
      },fail:function(err){
         reject(err)
      }
    })
  })
}


export default  {
  get,
  post,
  put,
  Delete,
  upload,
  request,
  fetch
}